package com.example.idealtv.client;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.util.Log;
import android.webkit.GeolocationPermissions;
import android.webkit.PermissionRequest;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import java.io.File;

public class MWebChromeClient extends WebChromeClient {
    private Activity activity;

    private ValueCallback<Uri[]> fileCb;

    public ValueCallback<Uri[]> getFileCb() {
        return fileCb;
    }

    public void setFileCb(ValueCallback<Uri[]> fileCb) {
        this.fileCb = fileCb;
    }

    public MWebChromeClient(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
        callback.invoke(origin,true,false);
        super.onGeolocationPermissionsShowPrompt(origin, callback);
    }


    @Override
    public void onGeolocationPermissionsHidePrompt() {
        super.onGeolocationPermissionsHidePrompt();
    }

    @Override
    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
        if (checkPermission()) {
            fileCb=filePathCallback;
            Intent selectionIntent = fileChooserParams.createIntent();
            Intent pthoneIntent = takePthone();
            Intent chooserIntent = Intent.createChooser(selectionIntent, "选择");
            chooserIntent.putExtra(
                    Intent.EXTRA_INITIAL_INTENTS,
                    new Intent[]{pthoneIntent}
            );
            return true;
        }
        return super.onShowFileChooser(webView,filePathCallback,fileChooserParams);
    }

    @NonNull
    private Intent takePthone() {
        Intent openCameraIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        openCameraIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        openCameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        Uri photoUri = FileProvider.getUriForFile(
                activity,
                activity.getPackageName() + ".fileprovider",
                createImageFile());
        //MainActivity.getInstance().setPathForCamera(photoUri);
        openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
        return openCameraIntent;
    }

    private File createImageFile() {
        /*String fileName = CommonUtils.getPhotoFileName() + MConstant.PICTURE_NAME;
        File file = FileUtil.createFile(FileUtil.PATH.ETC_PHOTO, fileName);
        return file;*/
        return null;
    }

    private boolean checkPermission(){
        /*int i = ContextCompat.checkSelfPermission(activity.getApplication(), Manifest.permission.CAMERA);
        if (i== PackageManager.PERMISSION_GRANTED){
            return true;
        }else {
            Toast.makeText(activity, "请授权后再试!", Toast.LENGTH_LONG).show();
            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.CAMERA},MConstant.PERMISSION_CAMERA_REQUEST_CODE);
            return false;
        }*/
        return true;
    }

}
